package com.castle.fortress.admin.goods.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.castle.fortress.admin.core.constants.GlobalConstants;
import com.castle.fortress.admin.goods.dto.GoodsParamsDto;
import com.castle.fortress.admin.goods.entity.GoodsParamsEntity;
import com.castle.fortress.admin.goods.service.GoodsParamsService;
import com.castle.fortress.admin.utils.ExcelUtils;
import com.castle.fortress.common.entity.DynamicExcelEntity;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.GlobalRespCode;
import com.castle.fortress.common.utils.ConvertUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 商品参数表 控制器
 *
 * @author majunjie
 * @since 2021-11-25
 */
@Api(tags="商品参数表管理控制器")
@Controller
public class GoodsParamsController {
    @Autowired
    private GoodsParamsService goodsParamsService;

    /**
     * 商品参数表的分页展示
     * @param goodsParamsDto 商品参数表实体类
     * @param current 当前页
     * @param size  每页记录数
     * @return
     */
    @ApiOperation("商品参数表分页展示")
    @GetMapping("/goods/goodsParams/page")
    @ResponseBody
    @RequiresPermissions("goods:goodsParams:pageList")
    public RespBody<IPage<GoodsParamsDto>> pageGoodsParams(GoodsParamsDto goodsParamsDto, @RequestParam(required = false) Integer current, @RequestParam(required = false)Integer size){
        Integer pageIndex= current==null? GlobalConstants.DEFAULT_PAGE_INDEX:current;
        Integer pageSize= size==null? GlobalConstants.DEFAULT_PAGE_SIZE:size;
        Page<GoodsParamsDto> page = new Page(pageIndex, pageSize);
        IPage<GoodsParamsDto> pages = goodsParamsService.pageGoodsParamsExtends(page, goodsParamsDto);

        return RespBody.data(pages);
    }

    /**
     * 商品参数表的列表展示
     * @param goodsParamsDto 商品参数表实体类
     * @return
     */
    @ApiOperation("商品参数表列表展示")
    @GetMapping("/goods/goodsParams/list")
    @ResponseBody
    public RespBody<List<GoodsParamsDto>> listGoodsParams(GoodsParamsDto goodsParamsDto){
        List<GoodsParamsDto> list = goodsParamsService.listGoodsParams(goodsParamsDto);
        return RespBody.data(list);
    }

    /**
     * 商品参数表保存
     * @param goodsParamsDto 商品参数表实体类
     * @return
     */
    @ApiOperation("商品参数表保存")
    @PostMapping("/goods/goodsParams/save")
    @ResponseBody
    @RequiresPermissions("goods:goodsParams:save")
    public RespBody<String> saveGoodsParams(@RequestBody GoodsParamsDto goodsParamsDto){
        if(goodsParamsDto == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        GoodsParamsEntity goodsParamsEntity = ConvertUtil.transformObj(goodsParamsDto,GoodsParamsEntity.class);
        if(goodsParamsService.save(goodsParamsEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 商品参数表编辑
     * @param goodsParamsDto 商品参数表实体类
     * @return
     */
    @ApiOperation("商品参数表编辑")
    @PostMapping("/goods/goodsParams/edit")
    @ResponseBody
    @RequiresPermissions("goods:goodsParams:edit")
    public RespBody<String> updateGoodsParams(@RequestBody GoodsParamsDto goodsParamsDto){
        if(goodsParamsDto == null || goodsParamsDto.getId() == null || goodsParamsDto.getId().equals(0L)){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        GoodsParamsEntity goodsParamsEntity = ConvertUtil.transformObj(goodsParamsDto,GoodsParamsEntity.class);
        if(goodsParamsService.updateById(goodsParamsEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 商品参数表删除
     * @param id
     * @return
     */
    @ApiOperation("商品参数表删除")
    @PostMapping("/goods/goodsParams/delete")
    @ResponseBody
    @RequiresPermissions("goods:goodsParams:delete")
    public RespBody<String> deleteGoodsParams(@RequestParam Long id){
        if(id == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if(goodsParamsService.removeById(id)) {
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }


    /**
     * 商品参数表批量删除
     * @param ids
     * @return
     */
    @ApiOperation("商品参数表批量删除")
    @PostMapping("/goods/goodsParams/deleteBatch")
    @ResponseBody
    @RequiresPermissions("goods:goodsParams:deleteBatch")
    public RespBody<String> deleteGoodsParamsBatch(@RequestBody List<Long> ids){
        if(ids == null || ids.size()<1){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if(goodsParamsService.removeByIds(ids)) {
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 商品参数表详情
     * @param id 商品参数表id
     * @return
     */
    @ApiOperation("商品参数表详情")
    @GetMapping("/goods/goodsParams/info")
    @ResponseBody
    @RequiresPermissions("goods:goodsParams:info")
    public RespBody<GoodsParamsDto> infoGoodsParams(@RequestParam Long id){
        if(id == null){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        GoodsParamsDto goodsParamsDto =  goodsParamsService.getByIdExtends(id);

        return RespBody.data(goodsParamsDto);
    }

	/**
     * 动态表头导出 依据展示的字段导出对应报表
     * @param dynamicExcelEntity
     * @param response
     * @throws Exception
     */
	@PostMapping("/goods/goodsParams/exportDynamic")
	@ApiOperation("动态表头导出，依据展示的字段导出对应报表")
	public void exportDynamic(@RequestBody DynamicExcelEntity<GoodsParamsDto> dynamicExcelEntity, HttpServletResponse response) throws Exception {
		List<GoodsParamsDto> list = goodsParamsService.listGoodsParams(dynamicExcelEntity.getDto());
		//字典、枚举、接口、json常量等转义后的列表数据 根据实际情况初始化该对象，null为list数据直接导出
		List<List<Object>> dataList = null;
		/**
        * 根据实际情况初始化dataList,可参照 com.castle.fortress.admin.system.controller.TmpDemoController类中方法：exportDynamic
        */
		ExcelUtils.exportDynamic(response,dynamicExcelEntity.getFileName()+".xlsx",null,list,dynamicExcelEntity.getHeaderList(),dataList);
	}


}
